package due.pc.pcshoot.sensor;

import java.util.Arrays;

import org.anddev.andengine.util.MathUtils;

public class ValueHolder {
	private static final float BLANK = 0;
	private int size;
	private float[] values;
	private float[] tempValues;
	private int position = 0;

	public ValueHolder(int size) {
		this.size = size;
		values = new float[size];
		tempValues = new float[size];
		clear();
	}

	public void clear() {
		Arrays.fill(values, BLANK);
		Arrays.fill(tempValues, BLANK);
	}

	public void add(float value) {
		values[position++ % size] = value;
	}

	/** get Middle Value or get Arithmatic Average Value */
	public float getMedian(boolean arithmaticAverage) {
		Arrays.fill(tempValues, BLANK);
		System.arraycopy(values, 0, tempValues, 0, size);

		if (arithmaticAverage) {
			return MathUtils.arrayAverage(tempValues);
		} else {
			Arrays.sort(tempValues);
			return tempValues[size / 2];
		}
	}
}